import requests
import xlsxwriter
from lxml import etree

name = input('请输入你想搜索的内容：')
url = f'https://hangzhou.zbj.com/search/f/?kw={name}'
print(url)
res = requests.get(url)
# print(res.text)

# 解析HTML的网页
html = etree.HTML(res.text)
# 拿到每一个服务商的div
divs = html.xpath('/html/body/div[6]/div/div/div[3]/div[5]/div[1]/div')
                #'/html/body/div[6]/div/div/div[3]/div[5]/div[1]/div[1]/div/div/a[1]/div[1]/p/text()'

workbook = xlsxwriter.Workbook(r'D:\ui\reptile\xpath\data\pig_website_data.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write(0, 0, '程序名称')  # 第i行0列
worksheet.write(0, 1, '价格') # 第i行1列
worksheet.write(0, 2, '公司名称')
worksheet.write(0, 3, '所在地址')

x = 0
for div in divs:  # 每一个服务商的信息
    price = div.xpath('./div/div/a[2]/div[2]/div[1]/span[1]/text()')[0].strip('¥') + '元'
    print(price)
    title = '小程序'.join(div.xpath('./div/div/a[2]/div[2]/div[2]/p/text()'))
    print(title)
    company_name = div.xpath('./div/div/a[1]/div[1]/p/text()')[1].replace('\n','')
    address = div.xpath('./div/div/a[1]/div[1]/div/span/text()')[0]
    # print(price_s)

#     j = x + 1
#     worksheet.write(j, 0, title)  # 第i行0列
#     worksheet.write(j, 1, price)  # 第i行1列
#     worksheet.write(j, 2, company_name)
#     worksheet.write(j, 3, address)
#
#     x += 1
#
# workbook.close()
#
# res.close()
# print('over!')